package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hu
 * @date 2025/5/24
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(queryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     */
    @Override
    @Transactional
    public void saveSetmeal(SetmealDTO setmealDTO) {
        //1. 保存基础套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);
        //2. 保存套餐中对应的菜品-批量
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insertSetmealDishBatch(setmealDishes);
    }

    /**
     * 页面回显
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishList = setmealDishMapper.getDishsById(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setSetmealDishes(setmealDishList);
        BeanUtils.copyProperties(setmeal, setmealVO);
        return setmealVO;

    }

    /**
     * 修改套餐
     */
    @Override
    public void updateById(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        //1. 修改套餐基本信息
        setmealMapper.updateById(setmeal);
        //2. 修改菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishMapper.deleteSetmealDishBatch(dto.getId());
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(dto.getId()));
        setmealDishMapper.insertSetmealDishBatch(setmealDishes);
    }

    /**
     * 删除套餐
     */
    @Override
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //一个循环做一件事情，新开一个循环。
        for (Long setmealId : ids) {
            //删除套餐
            setmealMapper.deleteSetmealById(setmealId);
            //同时删除关联的菜品
            setmealDishMapper.deleteBySetmealId(setmealId);
        }
    }

    /**
     * 套餐起售or停售
     */
    @Override
    public void startOrQuit(Integer status, Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        setmeal.setStatus(status);  //不要遗漏了状态丢进来
        if (status == StatusConstant.DISABLE) {
            setmealMapper.updateById(setmeal);
        } else if (status == StatusConstant.ENABLE) {
            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ? 其实联查得到dish集合遍历处理更快捷。
            List<SetmealDish> setmealDishList = setmealDishMapper.getDishsById(id);
            List<Long> dishIds = setmealDishList.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
            for (Long dishId : dishIds) {
                Dish dish = dishMapper.getById(dishId);
                Integer dishStatus = dish.getStatus();
                if (dishStatus == StatusConstant.DISABLE) {
                    throw new RuntimeException("套餐中存在禁用的菜品，无法启售");
                }
                setmealMapper.updateById(setmeal);
            }
        }

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
